package cn.lili.modules.goods_auth.serviceImpl;

import cn.lili.common.security.enums.SceneEnums;
import cn.lili.modules.goods.client.GoodsClient;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
import cn.lili.modules.goods.entity.enums.GoodsMarketEnum;
import cn.lili.modules.goods_auth.mapper.AuthSpaceGoodsMapper;
import cn.lili.modules.goods_auth.service.AuthSpaceGoodsService;
import cn.lili.modules.goods_auth.service.GoodsAuthSpaceService;
import cn.lili.modules.system.entity.dos.LiAuthSpace;
import cn.lili.modules.system.entity.dos.LiAuthSpaceGoods;
import cn.lili.modules.system.entity.dto.SpaceGoodsSearchParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AuthSpaceGoodsServiceImpl extends ServiceImpl<AuthSpaceGoodsMapper, LiAuthSpaceGoods> implements AuthSpaceGoodsService {

    @Resource
    private GoodsClient goodsClient;

    @Resource
    private GoodsAuthSpaceService goodsAuthSpaceService;

    /**
     * 空间商品分页列表
     *
     * @param searchParams 查询条件
     * @return 返回结果
     * */
    @Override
    public Page<LiAuthSpaceGoods> spaceGoods(SpaceGoodsSearchParams searchParams) {
        return this.page(new Page<>(searchParams.getPageNumber(), searchParams.getPageSize()), searchParams.queryWrapper());
    }

    /**
     * 添加空间商品
     * @param spaceGoods 对象
     * @return 返回结果
     * */
    @Override
    public Boolean addGoods(List<LiAuthSpaceGoods> spaceGoods) {
        return this.saveBatch(spaceGoods);
    }

    /**
     * 删除空间商品
     * @param list 对象
     * @return 返回结果
     * */
    @Override
    @Transactional
    @ShardingTransactionType(TransactionType.BASE)
    public Boolean delGoods(List<String> list) {
        return this.removeByIds(list);
    }

    /**
     * 选择商品
     * @param searchParams 对象
     * @return 返回结果
     * */
    @Override
    public Page<Goods> goodsList(GoodsSearchParams searchParams) {
        LambdaQueryWrapper<LiAuthSpaceGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LiAuthSpaceGoods::getAuthSpaceId,searchParams.getAuthSpaceId());
        queryWrapper.eq(LiAuthSpaceGoods::getDeleteFlag,false);
        List<LiAuthSpaceGoods> list = this.list(queryWrapper);

        if(list != null && !list.isEmpty()){
            List<String> notGoodsIds = list.stream().map(LiAuthSpaceGoods::getGoodsId).toList();
            searchParams.setNotGoodsIds(notGoodsIds);
        }

        searchParams.setScene(SceneEnums.STORE.name());
        searchParams.setAuthFlag(GoodsAuthEnum.PASS.name());
        searchParams.setMarketEnable(GoodsMarketEnum.UPPER.name());
        return goodsClient.queryByParams(searchParams);
    }

    /**
     * 根据商品ID查询空间商品
     * @param goodsId 商品ID
     * @return 商品空间列表
     * */
    @Override
    public List<LiAuthSpaceGoods> getLiAuthSpaceGoodsByGoodsId(String goodsId) {
        LambdaQueryWrapper<LiAuthSpaceGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LiAuthSpaceGoods::getGoodsId,goodsId);
        return this.list(queryWrapper);
    }

    /**
     * 根据商品ID更新商品
     * @param spaceGoods 对象
     * @return 返回结果
     * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateGoods(List<LiAuthSpaceGoods> spaceGoods) {
        return this.updateBatchById(spaceGoods);
    }

    /**
     * 获取空间商品列表
     * @return
     */
    @Override
    public List<Goods> getListByAuthSpace (String name) {
        LiAuthSpace authSpace = goodsAuthSpaceService.getByName(name);
        if (null != authSpace) {
            LambdaQueryWrapper<LiAuthSpaceGoods> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(LiAuthSpaceGoods::getAuthSpaceId, authSpace.getId());
            List<LiAuthSpaceGoods> spaceGoodsList = this.list(queryWrapper);
            if (!spaceGoodsList.isEmpty()) {
                List<String> goodsIds = spaceGoodsList.stream().map(LiAuthSpaceGoods::getGoodsId).toList();
                GoodsSearchParams goodsSearchParams = new GoodsSearchParams();
                goodsSearchParams.setAuthFlag(GoodsAuthEnum.PASS.name());
                goodsSearchParams.setMarketEnable(GoodsMarketEnum.UPPER.name());
                goodsSearchParams.setIds(goodsIds);
                return goodsClient.queryListByParams(goodsSearchParams);
            }
        }
        return new ArrayList<>();
    }
}
